package com.onlineclass.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.mvc.annotation.Controller;
import com.mvc.annotation.RequestMapping;
import com.mvc.core.ModelAndView;
import com.onlineclass.bean.User;
import com.onlineclass.dao.UserDAO;

@Controller
@RequestMapping("/user")
public class UserController {

	private UserDAO userDAO = new UserDAO();
	
	@RequestMapping("/reg.do")
	public ModelAndView reg(User user, HttpServletRequest request) {
		ModelAndView model = new ModelAndView("redirect:/index.jsp");
		userDAO.reg(user);
		request.getSession().setAttribute("user", userDAO.findByMobile(user.getMobile()));
		return model;
	}
	
	@RequestMapping("/login.do")
	public ModelAndView login(User user, HttpServletRequest request) {
		ModelAndView model = new ModelAndView("/login.jsp");
		Map<String,Object> map = new HashMap<String,Object>();	
		String msg = null;
		
		if(userDAO.login(user)==1) {
			request.getSession().setAttribute("user", userDAO.findByMobile(user.getMobile()));//把手机号放在sesison属性中
			model.setUrl("redirect:/index.jsp");
			return model;
		} else {
			msg = "登录信息有误";
			map.put("msg", msg);
		}
		model.setMap(map);
		return model;
	}
	
	@RequestMapping("/logout.do")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("user");
		return "redirect:/index.jsp";
	}
}
